﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using WindowsLive.Writer.Api;
using System.Windows.Forms;
using System.Diagnostics;

namespace LiveWriterBTFL
{
    public partial class BTFLEditor : SmartContentEditor
    {

        PluginSettings m_settings;
        ISmartContent m_content;
        public BTFLEditor()
        {
            InitializeComponent();
            this.SelectedContentChanged += new EventHandler(SelectedContentNowChanged);
            this.ContentEdited += BTFLEditor_ContentEdited;    
        }

        void BTFLEditor_ContentEdited(object sender, EventArgs e)
        {
            m_content = SelectedContent;
            m_settings = new PluginSettings(m_content.Properties);
            //textBox1.Text = m_settings.PlaceHolder;
            //textBox2.Text = m_settings.FinalText;
            MakePluginStrings();
        }
        void SelectedContentNowChanged(object sender, EventArgs e)
        {
            m_content = SelectedContent;
            m_settings = new PluginSettings(m_content.Properties);
            //textBox1.Text = m_settings.PlaceHolder;
            //textBox2.Text = m_settings.FinalText;
            UpdateContent(m_settings.FinalText);
        }

        private void UpdateContent(string textTosplit)
        {
            PluginHelper ph = new PluginHelper();
            txTwitterUserName.Text = ph.FindEditorStringProperty("T", textTosplit);
            txFB.Text = ph.FindEditorStringProperty("F", textTosplit);
            txLinkedInUser.Text = ph.FindEditorStringProperty("L", textTosplit);
            txBlog.Text = ph.FindEditorStringProperty("B", textTosplit);
        }

        private void btnSearchTwitter_Click(object sender, EventArgs e)
        {
            PluginHelper.OpenSearch("https://twitter.com/search?q=" + txTwitterUserName.Text);
        }

       

        private void button3_Click(object sender, EventArgs e)
        {
            PluginHelper.OpenSearch(PluginHelper.FB + txFB.Text);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            PluginHelper.OpenSearch("http://www.linkedin.com/vsearch/f?type=all&keywords=" + txLinkedInUser.Text.Replace(" ", "+"));
        }

       
        private void txTwitterUserName_TextChanged(object sender, EventArgs e)
        {
            try
            {
                MakePluginStrings();
               
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void MakePluginStrings()
        {
            PluginHelper ph = new PluginHelper();
            string TextToShow = ph.CreateBTFString(txTwitterUserName.Text, txFB.Text, txLinkedInUser.Text, txBlog.Text);
            string EditorText = ph.CreateBTFEditorString(txTwitterUserName.Text, txFB.Text, txLinkedInUser.Text, txBlog.Text);
            TextToShow = txPersonName.Text + " " + TextToShow;
            lblExample.Text = "Example: " + ph.Example;

            m_settings.PlaceHolder = TextToShow;

            m_settings.FinalText = EditorText;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            OnContentEdited();
        }

      
        
    }
}
